package com.lemner.motion.fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.TextView;

import com.lemner.motion.R;
import com.lemner.motion.base.BaseFragment;
import com.lemner.motion.util.FullScreen;

import java.util.ArrayList;
import java.util.List;

public class HistFragment extends BaseFragment {

     private OnFragmentInteractionListener mListener;
     private TabLayout tb_layout;
     private ViewPager vp_main;
    //当标签数目小于等于4个时，标签栏不可滑动
    public static final int MOVABLE_COUNT = 6;

    private int tabCount = 12;
    private List<String> tabs;
    private List<Fragment> fragments;

    public HistFragment() {
        // Required empty public constructor
    }

    @Override
    public int getLayoutFile() {
        return R.layout.fragment_hist;
    }

    @Override
    public void initView() {
        FullScreen.fullScreen(getActivity(),0x33131313);
         tb_layout=(TabLayout)view.findViewById(R.id.tb_layout);
         vp_main=(ViewPager)view.findViewById(R.id.vp_main);
        initDatas();
        initViewPager();
        initTabLayout();

    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {

    }


    private void initTabLayout() {
        //MODE_FIXED标签栏不可滑动，各个标签会平分屏幕的宽度
        tb_layout.setTabMode(tabCount <= MOVABLE_COUNT ? TabLayout.MODE_FIXED : TabLayout.MODE_SCROLLABLE);
        //指示条的颜色
        tb_layout.setSelectedTabIndicatorColor(0xFF46f69d);
        tb_layout.setSelectedTabIndicatorHeight(3);
        //关联tabLayout和ViewPager,两者的选择和滑动状态会相互影响
        tb_layout.setupWithViewPager(vp_main);
        //自定义标签布局
            for (int i = 0; i < tabs.size(); i++) {
                TabLayout.Tab tab = tb_layout.getTabAt(i);
                TextView tv = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tb_layout, tb_layout, false);
                tv.setGravity(Gravity.CENTER);
                tv.setText(tabs.get(i));
                tab.setCustomView(tv);
            }

    }

    private void initViewPager() {
        vp_main.setAdapter(new MyPagerAdapter(getActivity().getSupportFragmentManager()));
    }

    private void initDatas() {
        fragments = new ArrayList<>();
        tabs = new ArrayList<>();
            for (int i = 1; i <= tabCount; i++) {
                if (i < 10)
                    tabs.add("0" + i + "月");
                else
                    tabs.add(i + "月");
            }
            for (int i = 0; i < tabs.size(); i++)
                fragments.add(SonHistFragment.newInstance(tabs.get(i)));
        }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int position) {
                Bundle bundle = new Bundle();
                bundle.putInt("key", position);
                fragments.get(position).setArguments(bundle);
                return fragments.get(position);
        }
        @Override
        public int getCount() {
            return fragments.size();
        }
    }



    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }

}
